﻿using MediatR;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using wt.wine.Application.Common.Interfaces;
using wt.wine.Application.Common.Models;

namespace wt.wine.Application.ManageDevicetable.Queries
{
    public class GetManageDeviceListQueries:IRequest<Json<PaginatedList<GetManageDeviceListDTO>>>
    {
        public int pageIndex { get; set; }
        public int pageSize { get; set; }
    }

    public class GetManageDeviceListQueriesHandler : IRequestHandler<GetManageDeviceListQueries, Json<PaginatedList<GetManageDeviceListDTO>>>
    {
        private readonly ICurrentUserService _icurrentUserServices;
        private readonly IDevicetableRepository _idevicetableRepository;

        public GetManageDeviceListQueriesHandler(ICurrentUserService icurrentUserServices, IDevicetableRepository idevicetableRepository) {
            _icurrentUserServices = icurrentUserServices;
            _idevicetableRepository = idevicetableRepository;
        }
        public async Task<Json<PaginatedList<GetManageDeviceListDTO>>> Handle(GetManageDeviceListQueries request, CancellationToken cancellationToken)
        {
            PaginatedList<GetManageDeviceListDTO> model = new PaginatedList<GetManageDeviceListDTO>(new List<GetManageDeviceListDTO>(), 0, request.pageIndex, request.pageSize);
            if (request.pageIndex <= 0 || request.pageSize > 1000)
            {
                return Json<PaginatedList<GetManageDeviceListDTO>>.Failed("参数异常", model);
            }
            else {
                int userid = _icurrentUserServices.GetUserId();
                model = await _idevicetableRepository.GetManageDeviceList(userid, request.pageIndex, request.pageSize, cancellationToken);
                return Json<PaginatedList<GetManageDeviceListDTO>>.Success("查询成功", model);
            }
        }
    }
}
